package com.warmlight.voicepacket.utils;

import android.app.Activity;
import android.content.Context;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * @author  zkk
 * @data 2018/06/04
 */
public class ActivityUtil {

    /**
     * 检验context 是否销毁 解决You cannot start a load for a destroyed activity
     * @param context
     * @return
     */
    public static  boolean checkActivityIsDetroy(Context context){
        if(context==null){
            return true;
        }
        if(context instanceof Activity){
            if(((Activity) context).isDestroyed() || ((Activity) context).isFinishing()){
                return true;
            }
        }

        return false;
    }


    /**
     * 获取屏幕宽
     */
    public static int getWidth(Context context){
        WindowManager wm=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.widthPixels;
    }

    /**
     * 获取屏幕高
     */
    public static int getHeight(Context context){
        WindowManager wm=(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        return outMetrics.heightPixels;
    }

    public static int getStatusHeight(Context context){
        int result = 0;
        int resourceId = context.getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = context.getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }
}
